package com.bandapp.rss;

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.xml.sax.SAXException;

/*
 * This is a wrapper class around a DOM document that holds the RSSItems
 * So that we can easily get information in the DOM using this class
 * We now use TwitterRssItems, later on that can be abstracted to RSSItems
 */
public abstract class RSSWrapper<T> {

	private ArrayList<T> items;
	private URL urlFeed;
	private Document document;
	
	public RSSWrapper(URL urlFeed){
		items = new ArrayList<T>();
		this.urlFeed = urlFeed;
		makeDocument();
	}
	
	protected abstract void makeFeed();
	
	private void makeDocument(){
		
		URLConnection urlconn;
		
		try {
			/*
			 * we make the connection and a document is build , through parsing the xml stream
			 */
			urlconn = urlFeed.openConnection();
			DocumentBuilderFactory factory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			document = builder.parse(urlconn.getInputStream());
			makeFeed();

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	protected Document getDocument(){
		return this.document;
	}
	
	public ArrayList<T> getItems(){
		return this.items;
	}
	
	
}
